<template lang="pug">
  el-select(:value="valueTitle" :filterable="filterable" :filter-method="filterHandle"  :clearable="clearable" @clear="clearHandle")
    el-option(:value="valueTitle" :label="valueTitle")
      el-tree(
        style="overflow: hidden"
        id="tree-option"
        ref="selectTree"
        :accordion="accordion"
        :data="options"
        :props="props"
        :node-key="props.value"
        :filter-node-method="filterNode"
        :default-expanded-keys="defaultExpandedKey"
        @node-click="handleNodeClick"
      )
</template>
<script>

export default {
  name: 'SelectTree',
  props: {
    /* 配置项 */
    props: {
      type: Object,
      default: () => {
        return {
          value: 'id', // ID字段名
          label: 'name', // 显示名称
          children: 'children' // 子级字段名
        }
      }
    },
    /* 选项列表数据(树形结构的对象数组) */
    options: {
      type: Array,
      default: () => { return [] }
    },
    /* 初始值 */
    value: {
      type: [Number, String],
      default: () => { return null }
    },
    /* 可清空选项 */
    clearable: {
      type: Boolean,
      default: () => { return false }
    },
    // 医疗机构弹出框
    orgPopover: {
      type: Boolean,
      default: true
    },
    filterable: {
      type: Boolean,
      default: true
    },
    /* 自动收起 */
    accordion: {
      type: Boolean,
      default: () => { return false }
    },
    size: {
      type: String,
      default: 'medium'
    }
  },
  data () {
    return {
      valueId: this.value, // 初始值
      valueTitle: '',
      defaultExpandedKey: []
    }
  },
  mounted () {
    this.initHandle()
  },
  methods: {
    // 初始化值
    initHandle () {
      if (this.valueId) {
        this.valueTitle = this.$refs.selectTree.getNode(this.valueId).data[this.props.label] // 初始化显示
        this.$refs.selectTree.setCurrentKey(this.valueId) // 设置默认选中
        this.defaultExpandedKey = [this.valueId] // 设置默认展开
      } else {
        this.clearHandle()
      }
      this.$nextTick(() => {
        const scrollWrap = document.querySelectorAll('.el-scrollbar .el-select-dropdown__wrap')[0]
        const scrollBar = document.querySelectorAll('.el-scrollbar .el-scrollbar__bar')
        scrollWrap.style.cssText = 'margin: 0px; max-height: none; overflow: hidden;'
        scrollBar.forEach(ele => {
          ele.style.width = '6px'
        })
      })
    },
    // 切换选项
    handleNodeClick (node) {
      if (!node.disabled) {
        this.valueTitle = node[this.props.label]
        this.valueId = node[this.props.value]
        this.$emit('getValue', this.valueId)
        this.defaultExpandedKey = []
      }
    },
    // 清除选中
    clearHandle () {
      this.valueTitle = ''
      this.valueId = null
      this.defaultExpandedKey = []
      this.clearSelected()
      this.$emit('getValue', null)
    },
    /* 清空选中样式 */
    clearSelected () {
      const allNode = document.querySelectorAll('#tree-option .el-tree-node')
      allNode.forEach((element) => element.classList.remove('is-current'))
    },
    filterHandle (val) {
      this.$refs.selectTree.filter(val)
    },
    filterNode (value, data) {
      if (!value) return true
      return data.label.indexOf(value) !== -1
    }
  },
  watch: {
    value () {
      this.valueId = this.value
      this.initHandle()
    }
    // options (val) {
    //   console.log(val)
    // }
  }
}
</script>
<style lang="scss" scoped>

  .el-scrollbar .el-scrollbar__view .el-select-dropdown__item{
    height: auto;
    // max-height: 340px;
    padding: 0;
    overflow-x: hidden;
    overflow-y: hidden;
    box-sizing: border-box
  }
  .el-select-dropdown__item.selected{
    font-weight: normal;
  }
  ul li >>>.el-tree {
    overflow: hidden;
    .el-tree-node__content{
      height:auto;
      padding: 0 20px;
    }
  }
  .el-tree-node__label{
    font-weight: normal;
  }
  .el-tree >>>.is-current .el-tree-node__label{
    color: #409EFF;
    font-weight: 700;
  }
  .el-tree >>>.is-current .el-tree-node__children .el-tree-node__label{
    color:#606266;
    font-weight: normal;
  }

  .more{
    position: absolute;
    right: 2px;
    top:2px;
    padding: 9px;
    border: none;
    z-index: 10;
    min-width: auto !important;
  }
</style>
